package com.ruoyi.project.web.examReslut.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.OrderUtil;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.project.web.examPaper.domain.ExamPaper;
import com.ruoyi.project.web.examPaper.service.IExamPaperService;
import com.ruoyi.project.web.quesDetail.service.IQuesDetailService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.web.examReslut.domain.ExamReslut;
import com.ruoyi.project.web.examReslut.service.IExamReslutService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 考试结果Controller
 * 
 * @author ruoyi
 * @date 2021-01-29
 */
@Controller
@RequestMapping("/web/examReslut")
public class ExamReslutController extends BaseController
{
    private String prefix = "web/examReslut";

    @Autowired
    private IExamReslutService examReslutService;
    @Autowired
    private IExamPaperService examPaperService;
    @Autowired
    private IQuesDetailService quesDetailService;

    @RequiresPermissions("web:examReslut:view")
    @GetMapping()
    public String examReslut()
    {
        return prefix + "/examReslut";
    }

    /**
     * 查询考试结果列表
     */
    @RequiresPermissions("web:examReslut:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExamReslut examReslut)
    {
        startPage();
        List<ExamReslut> list = examReslutService.selectExamReslutList(examReslut);
        return getDataTable(list);
    }

    /**
     * 导出考试结果列表
     */
    @RequiresPermissions("web:examReslut:export")
    @Log(title = "考试结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExamReslut examReslut)
    {
        List<ExamReslut> list = examReslutService.selectExamReslutList(examReslut);
        ExcelUtil<ExamReslut> util = new ExcelUtil<ExamReslut>(ExamReslut.class);
        return util.exportExcel(list, "examReslut");
    }

    /**
     * 新增考试结果
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存考试结果
     */
    @RequiresPermissions("web:examReslut:add")
    @Log(title = "考试结果", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExamReslut examReslut)
    {
        return toAjax(examReslutService.insertExamReslut(examReslut));
    }

    /**
     * 修改考试结果
     */
    @GetMapping("/edit/{resultId}")
    public String edit(@PathVariable("resultId") Long resultId, ModelMap mmap)
    {
        ExamReslut examReslut = examReslutService.selectExamReslutById(resultId);
        mmap.put("examReslut", examReslut);
        return prefix + "/edit";
    }

    /**
     * 修改保存考试结果
     */
    @RequiresPermissions("web:examReslut:edit")
    @Log(title = "考试结果", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExamReslut examReslut)
    {
        return toAjax(examReslutService.updateExamReslut(examReslut));
    }

    /**
     * 删除考试结果
     */
    @RequiresPermissions("web:examReslut:remove")
    @Log(title = "考试结果", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(examReslutService.deleteExamReslutByIds(ids));
    }

    @PostMapping("/sendExamToUser")
    @ResponseBody
    @RepeatSubmit
    @Transactional
    public AjaxResult sendExamToUser(Long examId,String userIds){
        ExamPaper examPaper = examPaperService.selectExamPaperById(examId);
        String quesIds = examPaper.getQuesIds();
        List<ExamReslut> list =new ArrayList<>();
        int i = 0;
        try {
            String[] ids = userIds.split(",");
            for (String uid : ids) {
                long userId = Long.parseLong(uid);
                ExamReslut examReslut = new ExamReslut();
                examReslut.setUserId(userId);
                examReslut.setExamId(examId);
                examReslut.setLearnType(examPaper.getLearnType());
                List<ExamReslut> resluts = examReslutService.selectExamReslutList(examReslut);
                if (resluts.size()>0){
                    continue;
                }
                quesDetailService.insertSomeEntity(quesIds,userId,examId);
                list.add(examReslut);

            }
            if (list.size()>0){
                i = examReslutService.insertSomeEntity(list);
            }else {
                return error("无新的对象可下发");
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return error("系统内部错误");
        }
        return toAjax(i);
    }
}
